package com.xbongbong.pro.dataset.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Objects;

/**
 * @author gzt
 * @version 1.0
 * @Description -
 * @datetime - 2022/5/23-19:13
 * @since v1.0
 */
public class JsonObjectHelper {
    
    /**
     * @author  gzt
     * @Description
     * @Date 19:17 2022/5/23
     * @return com.alibaba.fastjson.JSONObject
     * @throws 
     **/
    
    public static JSONObject getJsonObject(HashMap<String, Object> map,String field) {
        if (StringUtils.isNotEmpty(field)){
            Object obj = map.get(field);
            if (Objects.nonNull(obj)){
                JSONObject jsonObject = (JSONObject) obj;
                return jsonObject;
            }
        }
        return null;
    }

    public static JSONArray getJsonArray(Object object) {
        if (Objects.nonNull(object) && (object instanceof JSONArray) ){
            return  (JSONArray)object;
        }else {
            return JSONArray.parseArray(JSON.toJSONString(object));
        }
    }

    public static Boolean getIsEmptyJsonArray(Object object) {
        boolean isEmpty = false;
        if (Objects.nonNull(object) && (object instanceof JSONArray) ){
            for (Object o : (JSONArray)object) {
                JSONObject filterJson = (JSONObject) o;
                if (filterJson.size() != 0) {
                    isEmpty = true;
                }
            }
        }
        return isEmpty;
    }



    public static JSONObject getJsonObject(Object object) {
        if (Objects.nonNull(object) && (object instanceof JSONObject) ){
            return  (JSONObject)object;
        }
        return null;
    }



}
